
package org.example.work.controller;
import org.example.work.pojo.Category;
import org.example.work.pojo.Result;
import org.springframework.web.bind.annotation.*;


import org.example.work.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


@RestController
@RequestMapping("/category")
@CrossOrigin
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param category 分类
     * @return Result
     */
    @PostMapping("/addCategory")
    public Result<Boolean> addCategory(@RequestBody Category category) {
        categoryService.addCategory(category);
        return Result.success(true);
    }

    /**
     * 删除分类
     * @param id 分类id
     * @return Result
     */
    @GetMapping("/deleteCategory/{id}")
    public Result<Boolean> deleteCategory(@PathVariable Integer id) {
        categoryService.deleteCategory(id);
        return Result.success(true);
    }

    /**
     * 根据id查询分类
     * @param id 分类id
     * @return Result
     */
    @GetMapping("/getCategoryById/{id}")
    public Result<Category> getCategoryById(@PathVariable Integer id) {
        Category category = categoryService.getCategoryById(id);
        return Result.success(category);
    }

    /**
     * 查询所有分类
     * @return Result
     */
    @GetMapping("/getCategoryList")
    public Result<List<Category>> getCategoryList() {
        List<Category> categoryList = categoryService.getCategoryList();
        return Result.success(categoryList);
    }

    /**
     * 修改分类
     * @param category 分类信息
     * @return Result
     */
    @PostMapping("/updateCategory")
    public Result<Boolean> updateCategory(@RequestBody Category category) {
        categoryService.updateCategory(category);
        return Result.success(true);
    }

}
